package com.ruoyi.security.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.security.domain.AfElevatorInfo;
import com.ruoyi.security.domain.ElevatorBaseInfo;
import com.ruoyi.security.domain.ElevatorInfoList;
import com.ruoyi.security.domain.ElevatorResult;
import com.ruoyi.security.elevator.util.ElevatorInterfaceUtil;
import com.ruoyi.security.service.IAfElevatorInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 电梯信息Controller
 *
 * @author felix
 * @date 2022-07-07
 */
@Api(tags = " 电梯基础信息")
@RestController
@RequestMapping("/afElevatorInfo")
public class AfElevatorInfoController extends BaseController {
    @Autowired
    private IAfElevatorInfoService afElevatorInfoService;

    @Autowired
    private RedisService redisService;

    @ApiOperation(value="电梯基础信息同步", notes="电梯基础信息同步")
    @PreAuthorize(hasPermi = "security:afElevatorInfo:list")
    @GetMapping("/syncElevatorInfo")
    public AjaxResult syncElevatorInfo() {

        String token = redisService.getCacheObject("elevator_token");
        if (StringUtils.isBlank(token)){
            return AjaxResult.error("梯联网token为空");
        }
        for (int page = 1;; page ++){
            ElevatorResult<ElevatorInfoList<ElevatorBaseInfo>> result = ElevatorInterfaceUtil.getElevatorInfoList(page +"", 50 +"", token,null);
            if ("0".equals(result.getErrorCode())){
                List<ElevatorBaseInfo> list = result.getData().getRows();
                //插入或者更新
                list.forEach(o ->{
                    afElevatorInfoService.insertOrUpdateInfo(o);
                });
                if (list.size() <50){
                    return AjaxResult.success();
                }
            }else {
                logger.error("error ==>" + result.getErrorMsg());
                return AjaxResult.error(result.getErrorMsg());
            }

        }

    }

    @ApiOperation(value="设置电梯事件回调地址", notes="设置电梯事件回调地址")
    @PostMapping("/setCallbackUrl")
    public AjaxResult setCallbackUrl(String url){
        //接口对应处理 ElevatorInterfaceController
        String token = redisService.getCacheObject("elevator_token");
        ElevatorResult result = ElevatorInterfaceUtil.setCallbackUrl(url,token);
        if(result.getErrorCode().equals("0")){
            return AjaxResult.success();
        }
        return AjaxResult.error("callbackUrl参数不能为空！");

    }

    /**
     * 查询电梯信息列表
     */
    @ApiOperation(value="查询电梯信息列表", notes="查询电梯信息列表")
    @PreAuthorize(hasPermi = "security:afElevatorInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(AfElevatorInfo afElevatorInfo) {
        startPage();
        List<AfElevatorInfo> list = afElevatorInfoService.selectAfElevatorInfoList(afElevatorInfo);
        return getDataTable(list);
    }

    /**
     * 导出电梯信息列表
     */
    @ApiOperation(value="导出电梯信息列表", notes="导出电梯信息列表")
    @PreAuthorize(hasPermi = "security:afElevatorInfo:export")
    @Log(title = "电梯信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfElevatorInfo afElevatorInfo) throws IOException {
        List<AfElevatorInfo> list = afElevatorInfoService.selectAfElevatorInfoList(afElevatorInfo);
        ExcelUtil<AfElevatorInfo> util = new ExcelUtil<AfElevatorInfo>(AfElevatorInfo.class);
        util.exportExcel(response, list, "电梯信息数据");
    }
    /**
     * 获取电梯信息详细信息
     */
    @ApiOperation(value="获取电梯信息详细信息", notes="获取电梯信息详细信息")
    @PreAuthorize(hasPermi = "security:afElevatorInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(afElevatorInfoService.selectAfElevatorInfoById(id));
    }

    /**
     * 新增电梯信息
     */
    @ApiOperation(value="新增电梯信息", notes="新增电梯信息")
    @PreAuthorize(hasPermi = "security:afElevatorInfo:add")
    @Log(title = "电梯信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AfElevatorInfo afElevatorInfo) {
        return toAjax(afElevatorInfoService.insertAfElevatorInfo(afElevatorInfo));
    }

    /**
     * 修改电梯信息
     */
    @ApiOperation(value="修改电梯信息", notes="修改电梯信息")
    @PreAuthorize(hasPermi = "security:afElevatorInfo:edit")
    @Log(title = "电梯信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfElevatorInfo afElevatorInfo) {
        return toAjax(afElevatorInfoService.updateAfElevatorInfo(afElevatorInfo));
    }

    /**
     * 删除电梯信息
     */
    @ApiOperation(value="删除电梯信息", notes="删除电梯信息")
    @ApiImplicitParam(name = "ids", value = "电梯信息ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afElevatorInfo:remove")
    @Log(title = "电梯信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(afElevatorInfoService.deleteAfElevatorInfoByIds(ids));
    }
}
